package com.loanapp.server.market.provider.modular.bbs.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.loanapp.base.common.exception.BusinessException;
import com.loanapp.base.common.utils.PhoneUtils;
import com.loanapp.base.common.utils.StringUtil;
import com.loanapp.base.database.common.base.SqlMapper;
import com.loanapp.server.market.api.bbs.entity.Comment;
import com.loanapp.server.market.api.bbs.query.CommentQuery;
import com.loanapp.server.market.provider.modular.bbs.mapper.CommentMapper;
import com.loanapp.server.market.provider.modular.bbs.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CommentServiceImpl implements CommentService {


    @Autowired
    private CommentMapper mapper;
    @Autowired
    private SqlMapper sqlMapper;

    @Override
    public List<Map<String, Object>> getCommentByPid(Long pid,int[] params) throws BusinessException {
        PageHelper.startPage(params[0],params[1]);
        String sql="select id,post_pname name,post_id pid,create_time  time,content from bbs_comment where post_id=#{pid} order by create_time DESC";
        List<Map<String, Object>> comment=sqlMapper.selectList(sql,pid);
        for(Map map : comment){
            String phone= (String) map.get("name");
           if(StringUtil.isNotEmpty(phone)){
             if(PhoneUtils.isMobile(phone)){
                 map.put("name",PhoneUtils.phoneWtihAsterisk(phone));
             }
            }
        }
        PageInfo pageInfo = new PageInfo(comment);
        if(params[0]>pageInfo.getPageNum()){
            comment=new ArrayList<>();
        }
        return comment;
    }

    @Override
    public Long Insert(Comment comment) throws BusinessException {
        Date date=new Date();
        comment.setCreateTime(date);
        comment.setUpdateTime(date);
        comment.setCommentType(1);
        comment.setIfshow(1);
        Integer rs=mapper.insert(comment);
        return rs>0?comment.getId():null;
    }

    @Override
    public boolean DeleteByPk(Long aLong) throws BusinessException {
        return false;
    }

    @Override
    public boolean Update(Comment comment) throws BusinessException {
        return false;
    }

    @Override
    public Comment selectByPk(Long aLong) throws BusinessException {
        return null;
    }

    @Override
    public Comment selectByModel(CommentQuery commentQuery) throws BusinessException {
        return null;
    }

    @Override
    public Comment selectBySelf(Comment comment) throws BusinessException {
        return null;
    }

    @Override
    public List<Comment> selectListByModel(CommentQuery commentQuery) throws BusinessException {
        return null;
    }

    @Override
    public Long selectCountByQueryMap(CommentQuery commentQuery) {
        return null;
    }

    @Override
    public List<Comment> selectPageListByQueryMap(CommentQuery commentQuery) throws BusinessException {
        return null;
    }
}
